<template>
  <div class="check-title">
    <el-space>
      <el-input placeholder="请输入搜索关键字" v-model="searchWord" />
      <el-button type="primary" icon="search">搜索</el-button>
      <el-divider direction="vertical" />
      <el-radio-group v-model="approverType">
        <el-radio-button label="全部" />
        <el-radio-button label="待审批" />
        <el-radio-button label="已通过" />
        <el-radio-button label="未通过" />
      </el-radio-group>
    </el-space>
  </div>
  <div class="check-table">
    <el-table border :data="pageCheckList">
      <el-table-column prop="applicantname" label="申请人" width="180" />
      <el-table-column prop="reason" label="审批事由" width="180" />
      <el-table-column prop="time" label="时间">
        <template #default="scope">
          {{ scope.row.time.join('-') }}
        </template>
      </el-table-column>
      <el-table-column prop="note" label="备注" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button @click="handlePutApply(scope.row._id, '已通过', scope.row.applicantid)" type="success" icon="check"
            size="small" circle />
          <el-button @click="handlePutApply(scope.row._id, '未通过', scope.row.applicantid)" type="danger" icon="close"
            size="small" circle />
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="180" />
    </el-table>
    <el-pagination small background layout="prev, pager, next" :total="checkList.length" :page-size="pageSize"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup lang="ts">
import { defineComponent, ref, computed } from 'vue'
import { useStore } from '@/store'
import { ElMessage } from 'element-plus'

defineComponent({
  name: 'CheckView'
})
const store = useStore()

const defaultType = '全部'
const approverType = ref(defaultType)
const searchWord = ref('')
const pageSize = ref(10)
const pageCurrent = ref(1)

const checkList = computed(() => store.state.checks.checkList.filter((v) =>
  (v.state === approverType.value || defaultType === approverType.value) && (v.note as string).includes(searchWord.value)))
const pageCheckList = computed(() => checkList.value.slice((pageCurrent.value - 1) * pageSize.value, pageCurrent.value * pageSize.value))
const handleCurrentChange = (current: number) => {
  pageCurrent.value = current
}

// 审批
const userInfo = computed(() => store.state.users.infos)
const handlePutApply = (_id: string, state: '已通过' | '未通过', applicantid: string) => {
  store.dispatch('checks/putApply', { _id, state }).then((res) => {
    if (res.data.errcode === 0) {
      store.dispatch('checks/getApply', { applicantid: userInfo.value._id }).then(res => {
        if (res.data.errcode === 0) {
          store.commit('checks/updateApplyList', res.data.rets)
        }
        store.dispatch('news/putRemind', { userid: applicantid, approver: true })
        ElMessage.success({ message: '审批成功', duration: 2000 })
      })
    }
  })
}
</script>

<style scoped lang="scss">
.check-title {
  margin: 20px;
  display: flex;
  justify-content: flex-end;
}

.check-table {
  margin: 10px;

  .el-pagination {
    float: right;
    margin: 10px;
  }
}
</style>